package org.itboys.weixin;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.weixin.service.WeixinConfigService;
import org.itboys.weixin.tools.HttpHelper;
import org.itboys.weixin.tools.WeixinUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
/**
 * 跳转到维系的首页
 * @author weisky
 *
 * 2016年4月17日
 */
@RestController 
@RequestMapping("/weixin") 
public class WexinController extends BaseController{

	@Autowired
	private WeixinConfigService weixinConfigService;
	
	/**
	 * 根据微信ID，获取微信配置信息
	 * @param weixinId
	 * @param response
	 */
	@RequestMapping("/getWeixinConfigById")
	public void getWeixinConfigById(Long weixinId, HttpServletResponse response) {
		try{
			AjaxUtils.renderJson(response, weixinConfigService.getById(weixinId));
		}catch(Exception e){
			e.printStackTrace();
			logger.error("WexinIndexController.getWeixinConfigById error!", e);
		}
		
	}
	
	/**
	 * 在授权页，获取当前微信用户信息
	 * @param appid
	 * @param secret
	 * @param grant_type
	 * @param code
	 * @param response
	 * @param request
	 */
	@RequestMapping("/getWxUserInfo")
	public void getWxUserInfo(String appid, String secret, String grant_type, String code,
			HttpServletResponse response, HttpServletRequest request){
		String callbackName = request.getParameter("callback");
		String access_token = "";
		String openid = "";
		try{
			if (code == null){
				AjaxUtils.renderJsonporJson(response, callbackName, ResultPageDOHelper.getErrorResultDO(-2, "error! code is null!"), new String[0]);
			}

			Map<String, Object> param_1 = Maps.newHashMapWithExpectedSize(4);
			param_1.put("appid", appid);
			param_1.put("secret", secret);
			param_1.put("code", code);
			param_1.put("grant_type", grant_type);
			
			String result = HttpHelper.doGet("https://api.weixin.qq.com/sns/oauth2/access_token", param_1);
			String errcode = String.valueOf(JSON.parseObject(result).get("errcode"));
			if (StringUtils.equals(errcode, "40029")){
				AjaxUtils.renderJsonporJson(response, callbackName, ResultPageDOHelper.getErrorResultDO(-3, "error! invalid code!"), new String[0]);
			}
			access_token = String.valueOf(JSON.parseObject(result).get("access_token"));
			WeixinUtils.cleanAccessToken(appid);
			//WeixinUtils.reSetAccessToken(appid, access_token);//重新设置缓存中的token值
			openid = (String) JSON.parseObject(result).get("openid");
			
			Map<String, Object> param_2 = Maps.newHashMapWithExpectedSize(3);
			param_2.put("access_token", access_token);
			param_2.put("openid", openid);
			param_2.put("lang", "zh_CN");
			
			String result_2 = HttpHelper.doGet("https://api.weixin.qq.com/sns/userinfo", param_2);
			AjaxUtils.renderJsonporJson(response, callbackName, ResultPageDOHelper.getMsgResultDO(result_2, "success!"), new String[0]);
		} catch (Exception e){
			this.logger.error("getShops error!", e);
			AjaxUtils.renderJsonporJson(response, callbackName, ResultPageDOHelper.getErrorResultDO(-1, "error!"), new String[0]);
		}
	}
	
	/**
	 * 跳到微信首页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(
			HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("/site/index");
	}

	/**
	 * 跳到微信第二个首页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index2")
	public ModelAndView index2(Model model,
			HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("/site/index2");
	}
}
